package cn.server.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * The Class LinuxBusiness.
 */
public class LinuxBusiness extends AbstractBusiness {

	/**
	 * Logger for this class.
	 *
	 * @param line
	 *            the line
	 * @return true, if checks if is iginore
	 */
	// private static final Log logger = LogFactory.getLog(LinuxBusiness.class);

	/** The ignore strs. */
	// private final String[] ignoreStrs = new String[] { "Listening", "#", "-",
	// "Total", "Peak", "Cumulative" };
	private static final String CLIENT_IP = "Client-Ip";
	
	private static final String CLIENT_TIME = "Send_Time";
	
	/**
	 * 判断是否为序号忽略的行,格式如:"Listening on eth0"或者
	 * "# Host name (port/service if enabled)"或者"-------".
	 *
	 * @param line
	 *            本行字符串
	 * @return true, if checks if is head
	 */
	public boolean isIginore(String line) {
		return StringUtils.isBlank(line);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.server.impl.AbstractBusiness#dealData(java.lang.String,
	 * java.util.Map)
	 */
	public List<Map<String, Object>> dealData(String line, Map<String, String> headMap) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2);
		// 带序号,源ip 目标IP 发送总数据 接收总数据 有可能是KB有可能是B
		String[] strs = StringUtils.split(line);
		Map<String, Object> map = new HashMap<String, Object>();
		// 暂时不读源IP,从头里读
		map.put("sourceip", headMap.get(CLIENT_IP));
		map.put("dataflowip", strs[0]);
		map.put("targetip", strs[1]);
		String datestr = StringUtils.defaultIfEmpty(headMap.get(CLIENT_TIME), new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss").format(new Date()));
		map.put("collecttime",
				new BigDecimal(datestr.replace("-", "").replace(" ", "").replace(":", "")));
		// 0:to;1:from
		Map<String, Object> dataMap = calculateIncoming(map, strs[3]);
		if (dataMap != null)
			list.add(dataMap);
		dataMap = calculateOutgoing(map, strs[2]);
		if (dataMap != null)
			list.add(dataMap);
		return list;
	}

	private Map<String, Object> calculateIncoming(Map<String, Object> map, String data) {
		return calculateData(map, data, 1);
	}

	private Map<String, Object> calculateOutgoing(Map<String, Object> map, String data) {
		return calculateData(map, data, 0);
	}

	private Map<String, Object> calculateData(Map<String, Object> map, String data, int direct) {
		if ("0B".equalsIgnoreCase(data) || "0KB".equalsIgnoreCase(data))
			return null;
		BigDecimal calulate = calulate(data);
		if (calulate == null)
			return null;
		HashMap<String, Object> retMap = new HashMap<String, Object>(map);
		retMap.put("dataflow", calulate);
		retMap.put("direct", direct);
		return retMap;
	}

	/**
	 * The main method.
	 *
	 * @param args
	 *            the args
	 * @throws Exception
	 *             the exception
	 */
	public static void main(String[] args) throws Exception {
		String[] test = { "aa", "bb", "cc" };
		String[] copy = Arrays.copyOfRange(test, 1, 3);
		System.out.println(copy.length);
		for (int i = 0; i < copy.length; i++) {
			System.out.println(copy[i]);
		}
	}
}